#include <iostream>

using std::cout;
using std::endl;

//1、友元类

//单例模式需不需要进行自动释放？
class Singleton
{
    friend class AutoRelease;
public:
    static Singleton *getInstance()
    {
        if(nullptr == _pInstance)
        {
            _pInstance = new Singleton();//堆对象
        }

        return _pInstance;
    }

    /* static void destroy() */
    /* { */
    /*     if(_pInstance) */
    /*     { */
    /*         delete _pInstance; */
    /*         _pInstance = nullptr; */
    /*     } */
    /* } */
private:
    Singleton()
    {
        cout << "Singleton()" << endl;
    }

    ~Singleton()
    {
        cout << "~Singleton()" << endl;
    }

private:
    static Singleton *_pInstance;
};

Singleton *Singleton::_pInstance = nullptr;

class AutoRelease
{
public:
    AutoRelease()
    {
        cout << "AutoRelease()" << endl;
    }

    ~AutoRelease()
    {
        cout << "~AutoRelease()" << endl;
        if(Singleton::_pInstance)
        {
            delete Singleton::_pInstance;
            Singleton::_pInstance = nullptr;
        }
    }
};

void test()
{
    /* Singleton::getInstance()->destroy(); */
    Singleton::getInstance();
    AutoRelease ar;//ar是栈对象
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

